<?php
/**
 * Profile Controller for Congregation Component
 * 
 * @package    Congregation
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * Profile Controller
 *
 * @package    Congregation
 * @subpackage Components
 */
class CongregationControllerProfile extends CongregationController
{
	
	
	/**
	 *
	 * @var _c_id (House id if defined)
	 */
	 
	protected $_c_id;
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	 
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
	
		$this->registerTask( 'add'  , 	'edit' );
		$this->registerTask( 'new'  , 	'edit' );
		$this->_c_id = JRequest::getCmd('c_id');
		
	}

	
	function display()
	{
		JRequest::setVar( 'view', 'profiles' );
		parent::display();
	}
	
	
	
	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'profile' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel('profile');
		
		
		if ($model->store()) {
			$msg = JText::_( 'Profile Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Profile' );
		}
		
		//if house id is defined
		$c_id = JRequest::getCmd('c_id');

		$link = 'index.php?option=com_congregation&controller=profile&c_id=' . $c_id;
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('profile');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Profile Could not be Deleted' );
		} else {
			$msg = JText::_( 'Profile Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_congregation&controller=profile&c_id=' . $this->_c_id, $msg );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_congregation&controller=profile&c_id=' . $this->_c_id, $msg );
	}
	
	function name()
	{
		if($username = JRequest::getVar('username'))
		{
			$model = $this->getModel('profile');
			if($user = $model->getUser(array('where' => array("username = '{$username}'"))))
			{
				
				list($fname,$lname) = preg_split('/\s+(?=[^\s]+$)/', $user->name, 2);
				$res = array('name' => $user->name, 'lname' => $lname, 'fname' => $fname);
				echo json_encode($res);
			}
		}
		
	}
	
	function updateall()
	
	{
				
		$this->_db	=& JFactory::getDBO();
		$query = "SELECT * FROM #__users";
		$this->_db->setQuery( $query );
		$rows = $this->_db->loadObjectList();
		

		foreach ($rows as $user)
		{
			
			list($fname,$lname) = preg_split('/\s+(?=[^\s]+$)/', $user->name, 2);

			$this->_db->setQuery( "UPDATE #__congregation_profile SET lname='" . $lname . "', fname = '" . $fname . "' WHERE username ='" . $user->username . "' LIMIT 1");
			$this->_db->query();
			
		}

		
	}
	
}